layui.use(['form', 'table', 'upload', 'laydate'], function () {
    var form = layui.form,
        layer = layui.layer,
        table = layui.table,
        laydate = layui.laydate,
        $ = layui.$,
        upload = layui.upload;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    // 当前弹出层，防止ID被覆盖
    var parentIndex = layer.index;

    //初始化
    $(function(){
        var id = $("#id").val();
        if(id != '' && id != null && id != undefined){
            var index = layer.load(2);
            var url = "/blogSpace/getUserInfoById";
            $.get(url, {}, function(data){
                layer.close(index);
                if(data.state == "200"){
                    $("#head_image").attr('src', '/head_photo/' + data['spaceUser'].headUrl);
                    $("#headUrl").val(data['spaceUser'].headUrl);
                    $("#userName").val(data['spaceUser'].userName);
                    $("#account").val(data['spaceUser'].account);
                    for(var i = 0; i < $("input[name='sex']").length; i++){
                        if(data['spaceUser'].sex == $($("input[name='sex']")[i]).val()){
                            $($("input[name='sex']")[i]).attr("checked", "checked");
                            break;
                        }
                    }
                    $("#professional").val(data['spaceUser'].professional);
                    $("#hobby").val(data['spaceUser'].hobby);
                    $("#age").val(data['spaceUser'].age);
                    $("#introduce").val(data['spaceUser'].introduce);
                    $("#bothday").val(data['spaceUser'].bothday.split(" ")[0]);
                    $("#tall").val(data['spaceUser'].tall);
                    $("#remark").val(data['spaceUser'].remark);
                    $("#phone").val(data['spaceUser'].phone);
                }else{
                    layer.msg(data.message, {icon:2});
                }
                form.render();
            })
        }
    });

    //监听提交
    form.on('submit(saveBtn)', function (data) {

        //验证两次新密码是否一致
        if(data.field.password != data.field.rePassword){
            layer.msg("两次新密码不一致", {icon: 2});
            return false;
        }

        var index = layer.load(2);
        var url = "/blogSpace/saveSpaceUser";
        $.post(url, data.field, function(res){
            if(res.state == "200"){
                layer.msg(res.message, {icon:6});
                //刷新
                setTimeout(function(){
                    location.reload();
                }, 2000);
            }else{
                layer.msg(res.message, {icon:2});
            }
            // 关闭弹出层
            layer.close(index);
            layer.close(parentIndex);
        }, 'json');

        return false;
    });

    //生日
    laydate.render({
        elem: '#bothday'
    });

    $("#closeBtn").on("click", function(){
        // 关闭弹出层
        layer.close(parentIndex);
    });

    //头像上传
    var uploadInst = upload.render({
        elem: '#headPhoto'
        ,url: '/blogSpace/uploadHeadPhoto' //改成您自己的上传接口
        ,data: { //额外参数
            spaceUserId: function(){
                return $('#id').val();
            }
        }
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#head_image').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //上传成功
            if(res.state == "200"){
                $("#headUrl").val(res.fileName);
                return layer.msg(res.message, {icon:1});
                //如果上传失败
            }else{
                return layer.msg(res.message, {icon:2});
            }
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#head_image_text');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs head-reload">重试</a>');
            demoText.find('.head-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });

});